package com.nou.bdm.controller;

import com.github.pagehelper.PageInfo;
import com.nou.bdm.model.NouEnumerationT;
import com.nou.bdm.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/level")
public class LevelController {
    @Autowired
    LevelService levelService;

    //简单的分页，分页链接没控制长度
//    @RequestMapping("/list")
//    public String  getLevelList(@RequestParam(defaultValue = "1",value = "pageNo") int pageNo,
//                               @RequestParam(defaultValue = "30" ,value = "pageSize") int pageSize, HttpSession session){
//
//        if(pageNo==-1){
//            int currtPage = (int) session.getAttribute("currentPage");
//            pageNo=currtPage-1;
//        }
//        if(pageNo==9999){
//            int currtPage = (int) session.getAttribute("currentPage");
//            pageNo=currtPage+1;
//        }
//        PageInfo<NouEnumerationT> pageData= levelService.getLevelList(pageNo, pageSize);
//        session.setAttribute("currentPage", pageData.getPageNum());
//        session.setAttribute("totalPage", pageData.getPages());
//        session.setAttribute("levels", pageData.getList());
//
//        return "index";
//    }

    @RequestMapping("/list")
    public String  getLevel(@RequestParam(defaultValue = "1",value = "pageNo") int pageNo,
                                @RequestParam(defaultValue = "10" ,value = "pageSize") int pageSize, HttpSession session){

//        if(pageNo==-1){
//            int currtPage = (int) session.getAttribute("currentPage");
//            pageNo=currtPage-1;
//        }
//        if(pageNo==9999){
//            int currtPage = (int) session.getAttribute("currentPage");
//            pageNo=currtPage+1;
//        }
        PageInfo<NouEnumerationT> pageData= levelService.getLevelList(pageNo, pageSize);
        session.setAttribute("currentPage", pageData.getPageNum());
        session.setAttribute("totalPage", pageData.getPages());
        session.setAttribute("levels", pageData.getList());

        return "index";
    }

    @RequestMapping("/add")
    public String  addLevel(NouEnumerationT nouEnumerationT,Model model){
       String msg= levelService.addLevel(nouEnumerationT);
        model.addAttribute("msg", msg);
        return "index";
    }

    @RequestMapping("/delete")
    public String  deleteLevel(String strId,Model model){
        String msg= levelService.deleteLevel(strId);
        model.addAttribute("msg", msg);
        return "index";
    }

    @RequestMapping("/update")
    public String  updateLevel(NouEnumerationT nouEnumerationT,Model model){
        String msg= levelService.updateLevel(nouEnumerationT);
        model.addAttribute("msg", msg);
        return "index";
    }
}
